package com.ymatou.iapi.refund.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;





import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.refund.parameter.CreateReturnBillBean;
import com.ymatou.iapi.refund.parameter.GetMaxRefundAmountBean;
import com.ymatou.iapi.refund.parameter.GetReturnInfoByReturnIdBean;
import com.ymatou.iapi.refund.parameter.GetReturnInfoByReturnIdResp;
import com.ymatou.iapi.refund.parameter.ProductCatalogModel;
import com.ymatou.iapi.refund.parameter.RefundLogVo;
import com.ymatou.iapi.refund.parameter.RefundProductVo;
import com.ymatou.iapi.refund.service.CreateReturnBillCall;
import com.ymatou.iapi.refund.service.GetMaxRefundAmountCall;
import com.ymatou.iapi.refund.service.GetReturnInfoByReturnIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.RefundCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.CS_ReturnApplyBill;
import com.ymttest.database.model.CS_ReturnApplyBillLog;
import com.ymttest.database.model.CS_ReturnApplyPictures;
import com.ymttest.database.model.CS_ReturnApplyProducts;
import com.ymttest.database.sqlwapper.CS_FastReturnLogWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillLogWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyPicturesWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyProductsWapper;
/**
*获取最大可退金额
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMaxRefundAmount{
	private static GetMaxRefundAmountBean bean;
	private static GetMaxRefundAmountCall call;
	private static CreateReturnBillCall createCall=new CreateReturnBillCall();
    private static CreateReturnBillBean createBean=new CreateReturnBillBean();
    private static CS_ReturnApplyBillWapper returnApplyBillWapper=new CS_ReturnApplyBillWapper();
	private static CS_ReturnApplyProductsWapper returnApplyProducts=new CS_ReturnApplyProductsWapper();
	private static CS_ReturnApplyPicturesWapper returnApplyPictures=new CS_ReturnApplyPicturesWapper();
	private static CS_ReturnApplyBillLogWapper returnApplyBillLog=new CS_ReturnApplyBillLogWapper();
	private static CS_FastReturnLogWapper fastReturnLog=new CS_FastReturnLogWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取最大可退金额");
	}

	@Before
	public void caseUp() {
		bean=new GetMaxRefundAmountBean();
		call=new GetMaxRefundAmountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMaxRefundAmount_001() {
		Logger.start(true,"正例-获取最大可退金额-一个订单一个商品");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			//createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();	
			
			BigDecimal payTotal=tradingServiceResp.getPayableAmount();
			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			verifyReturn(payTotal,fTotal);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMaxRefundAmount_002() {
		Logger.start(true,"正例-获取最大可退金额-一个订单多个商品，退其中一个商品");
		try {
			
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillMoreBean_AcceptOrder(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();	
			
			BigDecimal productprice=tradingServiceResp.getOrderList().get(0).getOrderDetailPoList().get(0).getProductPrice();
			int productCount=tradingServiceResp.getOrderList().get(0).getOrderDetailPoList().get(0).getProductCount();

			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			BigDecimal payTotal=productprice.multiply(new BigDecimal(productCount)).add(fTotal);
			verifyReturn(payTotal,fTotal);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetMaxRefundAmount_003() {
		Logger.start(true,"正例-获取最大可退金额-多个商品（订单纬度）");
		try {
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillMoreBean_AcceptOrder(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();	
			
			BigDecimal productprice=tradingServiceResp.getOrderList().get(0).getOrderDetailPoList().get(0).getProductPrice();
			int productCount=tradingServiceResp.getOrderList().get(0).getOrderDetailPoList().get(0).getProductCount();

			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			BigDecimal payTotal=productprice.multiply(new BigDecimal(productCount)).add(fTotal);
			verifyReturn(payTotal,fTotal);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMaxRefundAmount_004() {
		Logger.start(true,"正例-获取最大可退金额-订单不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(9999999);
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals(true,call.getMsg1().contains("计算商品最大可退金额失败"), "验证Msg");
			Logger.verifyEquals("201",call.getStatus(), "验证Status");
			Logger.verifyEquals("1",call.getbCode(), "验证bCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMaxRefundAmount_005() {
		Logger.start(true,"正例-获取最大可退金额-退单Id为空");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId("");
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();	
			
//			Logger.verifyEquals("",call.getMsg1(), "验证Msg");
//			Logger.verifyEquals("400",call.getStatus(), "验证Status");
//			Logger.verifyEquals("0",call.getbCode(), "验证bCode");	
			BigDecimal payTotal=tradingServiceResp.getPayableAmount();
			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			verifyReturn(payTotal,fTotal);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMaxRefundAmount_006() {
		Logger.start(true,"正例-获取最大可退金额-退单Id不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId("99999");
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();		
			
			BigDecimal payTotal=tradingServiceResp.getPayableAmount();
			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			verifyReturn(payTotal,fTotal);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMaxRefundAmount_007() {
		Logger.start(true,"正例-获取最大可退金额-买家用户Id不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(99999);
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();			
			
//			Logger.verifyEquals("",call.getMsg1(), "验证Msg");
//			Logger.verifyEquals("400",call.getStatus(), "验证Status");
//			Logger.verifyEquals("0",call.getbCode(), "验证bCode");
			BigDecimal payTotal=tradingServiceResp.getPayableAmount();
			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			verifyReturn(payTotal,fTotal);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetMaxRefundAmount_008() {
		Logger.start(true,"正例-获取最大可退金额-退货商品列表数据不存在(与研发讨论，这个case 不存在)");
		try {
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
			model.setCatalogId("999");
			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();			
			
//			Logger.verifyEquals("",call.getMsg1(), "验证Msg");
//			Logger.verifyEquals("400",call.getStatus(), "验证Status");
//			Logger.verifyEquals("0",call.getbCode(), "验证bCode");
			BigDecimal payTotal=tradingServiceResp.getPayableAmount();
			BigDecimal fTotal=tradingServiceResp.getOrderList().get(0).getOrderFreight();
			verifyReturn(payTotal,fTotal);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetMaxRefundAmount_009() {
		Logger.start(true,"正例-获取最大可退金额-退货列表为空(与研发讨论，这个case 不存在)");
		try {
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);

			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setOrderId(createBean.getOrderId());
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			
			ProductCatalogModel model=new ProductCatalogModel();
			List<ProductCatalogModel> plist=new ArrayList();
//			model.setCatalogId(createCall.getCallData().getReturnBillList().get(0).getCatalogId());
//			model.setReturnCount(createCall.getCallData().getReturnBillList().get(0).getReturnCount());
			plist.add(model);
			bean.setProducts(plist);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals(true,call.getMsg1().contains("请传入商品规格Id"), "验证Msg");
			Logger.verifyEquals("400",call.getStatus(), "验证Status");
			Logger.verifyEquals("12",call.getbCode(), "验证bCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(BigDecimal payTotal,BigDecimal fTotal) throws JSONException
	{
		JSONObject resp=call.getRetData();
		
		Logger.verifyEquals("",call.getMsg1(), "验证Msg");
		Logger.verifyEquals("200",call.getStatus(), "验证Status");
		Logger.verifyEquals("0",call.getbCode(), "验证bCode");
		
		Logger.verifyEquals(0,resp.get("errorCode"), "验证errorCode");
		Logger.verifyEquals("",resp.get("errorMessage"), "验证errorMessage");
		Logger.verifyEquals(payTotal.stripTrailingZeros().toPlainString(),resp.get("MaxReturnAmount").toString(), "验证MaxReturnAmount");
		Logger.verifyEquals(fTotal.stripTrailingZeros().toPlainString(),resp.get("MaxReturnFreihtAmount").toString(), "验证MaxReturnFreihtAmount");
		
	}	
}